﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace iPhotoUploader.Library
{
    public class AlbumFace
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Name { get; set; }
        public Guid PeopleId { get; set; }
        public AlbumImage ListingImage { get; set; }

        public AlbumFace(XmlNode node)
        {
            if (node.SelectSingleNode("key[normalize-space(text())='key']") != null)
                Id = Int32.Parse(node.SelectSingleNode("key[normalize-space(text())='key']").NextSibling.InnerText);

            if (node.SelectSingleNode("key[normalize-space(text())='name']") != null)
                Name = node.SelectSingleNode("key[normalize-space(text())='name']").NextSibling.InnerText;

            if (!string.IsNullOrEmpty(Name))
            {
                string[] name = Name.Split(' ');
                FirstName = name.Length > 0 ? name[0] : string.Empty;
                LastName = name.Length > 1 ? name[1] : string.Empty;
            }
        }

        public static List<AlbumFace> LoadAllFaces(XmlDocument xml)
        {
            List<AlbumFace> allFaces = new List<AlbumFace>();
            XmlNodeList masterFaces = xml.SelectSingleNode("plist/dict/key[normalize-space(text())='List of Faces']").NextSibling.ChildNodes;

            foreach (XmlNode masterFace in masterFaces)
            {
                if (masterFace.Name == "dict")
                {
                    allFaces.Add(new AlbumFace(masterFace));
                }
            }

            return allFaces;
        }
    }
}
